package com.lecture.java8.excel.dyr.i;

import com.alibaba.excel.EasyExcel;
import com.lecture.java8.excel.DemoDataListener;
import com.lecture.java8.excel.dyr.h.Model0725;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Test0726 {

    public static void main(String[] args) {
        String dir = "D:\\study\\tmp\\0726\\";



        String result = dir + "上市公司排污费和环境保护税数据（1990-2022）.xlsx";
        DemoDataListener<Model0726> dataResultListener1 = new DemoDataListener<>();
        EasyExcel.read(result, Model0726.class,dataResultListener1).sheet().doRead();
        //全要素生产率
        List<Model0726> resultList1 = dataResultListener1.getDataList();

        Map<String,Model0726> result1Map = new HashMap<>();
        for (Model0726 model0726 : resultList1) {
            result1Map.put(model0726.getA() + "-" + model0726.getC(),model0726);
        }

        String result2 = dir + "【更新2022】上市公司常用控制变量和异质性分组2000-2022  (目录).xlsx";
        DemoDataListener<Model0726_2> dataResultListener2 = new DemoDataListener<>();
        EasyExcel.read(result2, Model0726_2.class,dataResultListener2).sheet().doRead();
        //全要素生产率
        List<Model0726_2> resultList2 = dataResultListener2.getDataList();

        Map<String,Model0726_2> result2Map = new HashMap<>();
        for (Model0726_2 model0726 : resultList2) {
            result2Map.put(model0726.getA() + "-" + model0726.getB(),model0726);
        }

        String result3 = dir + "最终结果-企业生命周期.xlsx";
        DemoDataListener<Model0726_3> dataResultListener3 = new DemoDataListener<>();
        EasyExcel.read(result3, Model0726_3.class,dataResultListener3).sheet().doRead();
        //全要素生产率
        List<Model0726_3> resultList3 = dataResultListener3.getDataList();

        Map<String,Model0726_3> result3Map = new HashMap<>();
        for (Model0726_3 model0726 : resultList3) {
            result3Map.put(model0726.getA() + "-" + model0726.getC(),model0726);
        }


        String result4 = dir + "营业收入.xlsx";
        DemoDataListener<Model0726_4> dataResultListener4 = new DemoDataListener<>();
        EasyExcel.read(result4, Model0726_4.class,dataResultListener4).sheet().doRead();
        //全要素生产率
        List<Model0726_4> resultList4 = dataResultListener4.getDataList();

        Map<String,Model0726_4> result4Map = new HashMap<>();
        for (Model0726_4 model0726 : resultList4) {
            result4Map.put(model0726.getA() + "-" + model0726.getC(),model0726);
        }


        String resultPath = dir + "2013年带行业变量的.xlsx";
        DemoDataListener<Result0726> dataResultListener = new DemoDataListener<>();
        EasyExcel.read(resultPath, Result0726.class,dataResultListener).sheet().doRead();
        //全要素生产率
        List<Result0726> resultList = dataResultListener.getDataList();
        for (Result0726 model0724 : resultList) {
            Model0726 model0726 = result1Map.get(model0724.getA() + "-" + model0724.getC());
            if (model0726 != null) {
                model0724.setNn(model0726.getN());
            }
            Model0726_2 m2 = result2Map.get(model0724.getA() + "-" + model0724.getC());
            if (m2 != null) {
                model0724.setDd(m2.getD());
                model0724.setEe(m2.getE());
            }
            Model0726_3 m3 = result3Map.get(model0724.getA() + "-" + model0724.getC());
            if (m3 != null) {
                model0724.setDdd(m3.getD());
                model0724.setEeee(m3.getE());
                model0724.setFfff(m3.getF());
                model0724.setGgg(m3.getG());
            }

            Model0726_4 m4 = result4Map.get(model0724.getA() + "-" + model0724.getC());
            if (m3 != null) {
                model0724.setYysr(m4.getD());
            }
        }



        EasyExcel.write( dir + "0726-2结果.xlsx", Result0726.class)
                .sheet("a").doWrite(resultList);

    }



    public static void main2(String[] args) {
        String dir = "D:\\study\\tmp\\0725\\";


        String resultPath = dir + "2011年开始的原始数据.xlsx";
        DemoDataListener<Model0725> dataResultListener = new DemoDataListener<>();
        EasyExcel.read(resultPath, Model0725.class,dataResultListener).sheet().doRead();
        //全要素生产率
        List<Model0725> resultList = dataResultListener.getDataList();
        Set<String> codeSet = new HashSet<>();
        for (Model0725 model0724 : resultList) {
            if (model0724.getB().contains("st") || model0724.getB().contains("ST")) {
                codeSet.add(model0724.getA());
            }
        }
        List<Model0725> result1 = new ArrayList<>();
        for (Model0725 model0724 : resultList) {
            if (!codeSet.contains(model0724.getA())) {
                result1.add(model0724);
            }
        }
        result1 = filterCompleteYears(result1,2013,2022);



        EasyExcel.write( dir + "0725结果.xlsx", Model0725.class)
                .sheet("a").doWrite(result1);

    }

    public static List<Model0725> filterCompleteYears(List<Model0725> list, int startYear, int endYear) {
        Set<Integer> requiredYears = IntStream.rangeClosed(startYear, endYear)
                .boxed()
                .collect(Collectors.toSet());

        // Group by code and filter
        Map<String, Set<Integer>> codeToYearsMap = list.stream()
                .collect(Collectors.groupingBy(
                        Model0725::getA,
                        Collectors.mapping(Model0725::getC, Collectors.toSet())
                ));


        Set<String> validCodes = codeToYearsMap.entrySet().stream()
                .filter(entry -> entry.getValue().equals(requiredYears))
                .map(Map.Entry::getKey)
                .collect(Collectors.toSet());

        // Filter original list by valid codes
        return list.stream()
                .filter(obj -> validCodes.contains(obj.getA()))
                .collect(Collectors.toList());
    }
}
